﻿using Firefly.Framework.Components;
using Firefly.Framework.ContentManagement;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Firefly.Framework.Graphics
{
    public class TexturedShader : ShaderProxy, IShaderProxy
    {
        public Texture2D Texture
        {
            get { return Effect.Parameters["Texture"].GetValueTexture2D(); }
            set { Effect.Parameters["Texture"].SetValue(value); }
        }
        public float Alpha
        {
            get { return Effect.Parameters["Alpha"].GetValueSingle(); }
            set { Effect.Parameters["Alpha"].SetValue(value); }
        }
        public QuadRenderer QuadRenderer { get; set; }

        public TexturedShader(IDrawable parent)
            : base(parent)
        {
        }

        #region IShaderProxy Members

        public override void Draw(GameTime gameTime)
        {
            Effect.Begin();

            foreach (EffectPass pass in Effect.CurrentTechnique.Passes)
            {
                pass.Begin();
                QuadRenderer.Draw(gameTime);
                pass.End();
            }

            Effect.End();
        }

        #endregion

        #region IHasContent Members

        public override void LoadContent()
        {
            EffectName = "Effects/Textured";
            Effect = ContentService.Instance.Load<Effect>(EffectName);
            Effect.CurrentTechnique = Effect.Techniques["Render"];
        }

        public override void UnloadContent()
        {
            ContentService.Instance.Release(EffectName);
        }

        #endregion

        #region IDisposable Members

        ~TexturedShader()
        {
            Dispose(false);
        }

        #endregion
    }
}
